package com.wtpaint.tree;


public class RBTree<K extends Comparable<K>, V> {
    private static final boolean RED = true;
    private static final boolean BLACK = false;

    private class Node {
        public K key;
        public V value;
        public Node left, right;
        public boolean color;

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
            left = null;
            right = null;
            color = RED;
        }
    }

    private Node root;
    private int size;

    //判断结点node的颜色
    private boolean isRed(Node node) {
        if (null == node) {
            return BLACK;
        }
        return node.color;
    }

    //颜色翻转
    private void flipColors(Node node) {
        node.color = RED;
        node.left.color = BLACK;
        node.right.color = BLACK;
    }

    //红黑树的左旋转
    private Node leftRotate(Node node) {
        Node x = node.right;
        //左旋转
        node.right = x.left;
        x.left = node;

        x.color = node.color;
        node.color = RED;
        return x;
    }

    //红黑树的右旋转
    private Node rightRotate(Node node) {
        Node x = node.left;
        node.left = x.right;
        x.right = node;

        x.color = node.color;
        node.color = RED;

        return x;
    }

    public void add(K key, V value) {
        root = add(root, key, value);
    }

    private Node add(Node node, K key, V value) {
        if (null == node) {
            size++;
            node = new Node(key, value);
            return node;
        }
        if (key.compareTo(node.key) < 0) {
            node.left = add(node.left, key, value);
        } else if (key.compareTo(node.key) > 0) {
            node.right = add(node.right, key, value);
        } else {
            node.value = value;
        }

        if (isRed(node.right) && !isRed(node.left)) {
            node = leftRotate(node);
        }

        if (isRed(node.left) && isRed(node.left.left)) {
            node = rightRotate(node);
        }

        if (isRed(node.right) && isRed(node.left)) {
            flipColors(node);
        }

        return node;
    }

    private Node getNode(Node node, K key) {
        if (null == node) {
            return null;
        }

        if (key.compareTo(node.key) == 0) {
            return node;
        } else if (key.compareTo(node.key) > 0) {
            return getNode(node.right, key);
        } else {
            return getNode(node.left, key);
        }
    }

    public boolean contains(K key) {
        return getNode(root, key) != null;
    }

    public V get(K key) {
        return getNode(root, key).value;
    }

    public void set(K key, V newValue) {
        Node node = getNode(root, key);
        if (node == null) {
            throw new IllegalArgumentException("no value with your key:" + key);
        }
        node.value = newValue;
    }

//    public V remove(K key) {
//
//        Node node = getNode(root, key);
//        if (node != null) {
//            root = remove(root, key);
//            return node.value;
//        }
//        return null;
//    }
//
//    private Node remove(Node node, K key) {
//
//        if (null == node) {
//            return null;
//        }
//
//        if (key.compareTo(node.key) > 0) {
//            node.right = remove(node.right, key);
//            return node;
//        } else if (key.compareTo(node.key) < 0) {
//            node.left = remove(node.left, key);
//            return node;
//        } else {
//            if (node.left == null) {
//                Node rightNode = node.right;
//                node.right = null;
//                size--;
//                return rightNode;
//            }
//            if (node.right == null) {
//                Node rightNode = node.left;
//                node.left = null;
//                size--;
//                return rightNode;
//            }
//            Node successor = minmum(node.right);
//            successor.right = removeMin(node.right);
//            successor.left = node.left;
//            node.left = node.right = null;
//            return successor;
//        }
//    }

//    private Node minmum(Node node) {
//        if (node.left == null) {
//            return node;
//        }
//        return minmum(node.left);
//    }
//
//    //获取到删除后新的搜索树的结点
//    private Node removeMin(Node node) {
//        if (node.left == null) {
//            Node rightNode = node.right;
//            node.right = null;
//            size--;
//            return rightNode;
//        }
//        node.left = removeMin(node.left);
//        return node;
//    }

    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }
}
